<?php
/**
 * 角色核心类
 * User: Administrator
 * Date: 2018\9\6 0006
 * Time: 17:12
 */

namespace app\Lib\Model\Role;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\Role\RoleJudgeHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\User\User;
use think\Cache;

class Role implements RoleHandle
{
    /**
     * @var string
     */
    private $cachePrefix ;
    /**
     * @var \app\Lib\Model\DbModel\Role
     */
    private $Db;
    /**
     * @var
     */
    private $roleId;
    /**
     * @var bool
     */
    private $cache;

    /**
     * Role constructor.
     * @param $roleId
     * @param bool $cache
     */
    public function __construct($roleId, $cache = true)
    {
        $this->cachePrefix ='role_Id_'.$roleId.'_';
        $this->Db = new \app\Lib\Model\DbModel\Role();
        $this->roleId = $roleId;
        $this->cache  = $cache ;
    }

    /**
     * 获取别名
     * @return string
     */
    public function getRoleAlias()
    {
        return $this->getFieldValue($this->Db->_role_alias);
    }

    /**
     * @return mixed
     */
    public function getRoleId()
    {
        return $this->roleId;
    }

    /**
     * 获取等级
     * @return int
     */
    public function getLevel()
    {
        return $this->getFieldValue($this->Db->_level);
    }


    /**
     * 获取父级类
     * @return Role|bool
     */
    public function getParentClass()
    {
        $result = false;
        $roleId = $this->getParentId();
        if($roleId>0){
            $result =  new Role($roleId);
        }
        return $result;
    }

    /**
     * 获取角色名称
     * @return string
     */
    public function getName()
    {
        return $this->getFieldValue($this->Db->_name);
    }

    /**
     * 获取状态中文名称
     * @return string
     */
    public function getStatusStr()
    {
        $str       = [0=>'禁用',1=>'正常'];
        $status    = $this->getStatus();
        $statusStr = $str[$status];
        return $statusStr;
    }

    /**
     * 获取创建时间
     * @return string
     */
    public function getCreateTime()
    {
        return $this->getFieldValue($this->Db->_create_time);
    }

    /**
     * 获取创建人对象
     * @return UserHandle
     */
    public function getCreateUserObj()
    {
        $uid = $this->getFieldValue($this->Db->_create_uid);
        return new User($uid);
    }

    /**
     * 获取修改人对象
     * @return UserHandle
     */
    public function getSaveUserObj()
    {
        $uid = $this->getFieldValue($this->Db->_save_uid);
        return new User($uid);

    }

    /**
     * 获取修改时间
     * @return string
     */
    public function getSaveTime()
    {
        return $this->getFieldValue($this->Db->_save_time);
    }

    /**
     * 获取排序
     * @return int
     */
    public function getListOrder()
    {
        return $this->getFieldValue($this->Db->_list_order);
    }

    /**
     * 获取备注
     * @return string
     */
    public function getRemark()
    {
        return $this->getFieldValue($this->Db->_remark);
    }

    /**
     * 获取创建人名称
     * @return string
     */
    public function getCreateName()
    {
        return ($this->getCreateUserObj())->getAccount();
    }

    /**
     * 获取修改人
     * @return string
     */
    public function getSaveUserName()
    {
        $model =  $this->getSaveUserObj();
        if($model instanceof  UserHandle){
            return $model->getAccount();
        }
        return '无';
    }

    /**
     * 删除类的缓存
     * @return bool
     * @throws BlException
     */
    public function delAllCache()
    {
        $result =  $this->Db->getTableFields();
        foreach ($result as $val){
            $this->delSingleCache($val);
        }
        return true;
    }

    /**
     * 角色判断类
     * @return RoleJudgeHandle
     */
    public function getRoleJudgeClass()
    {
        return new RoleJudge(new Role($this->roleId));
    }


    /**
     * 删除单个字段缓存
     * @param $field
     * @return bool
     * @throws BlException
     */
    public function delSingleCache($field)
    {
        if(empty($field)){
            throw new BlException('请提供要清除缓存的字段');
        }
        $cacheName = $this->getCacheName($field);
        cache($cacheName,null);
        return true;
    }

    /**
     * 获取某个字段的值
     * @param $filed
     * @return string
     */
    private function getFieldValue($filed){
        $result = false;
        if($this->cache){
            $result = $this->getCacheValue($filed);
        }
        if($result === false){
            $result = $this->getDbFieldValue($filed);
        }
        return $result;
    }

    /**
     * @param $filed
     * @return mixed|string
     */
    private function getCacheValue($filed){
        $result = cache($this->getCacheName($filed));
        if(empty($result)){
            $result = $this->getDbFieldValue($filed);
            $result = empty($result)?'无':$result;
            cache($this->getCacheName($filed),$result,1800);
        }
        return $result;
    }

    /**
     * @param $filed
     * @return mixed
     */
    private function getDbFieldValue($filed){
        return $this->Db->where($this->Db->_id,$this->roleId)->value($filed);
    }

    /**
     * 获取缓存名称
     * @param $filed
     * @return string
     */
    private function getCacheName($filed){
        return $this->cachePrefix.$filed;
    }

    /**
     * 角色状态
     * @return int
     */
    private function getStatus(){
        $status = $this->getFieldValue($this->Db->_status);
        return $status;
    }

    /**
     * 获取父级id
     * @return int
     */
    private function getParentId()
    {
        return $this->getFieldValue($this->Db->_parent_id);
    }

    /**
     * 获取后台是否可以登录
     * @return string
     */
    private function getIsAdminLogin(){
        return $this->getFieldValue($this->Db->_is_admin_login);
    }
    /**
     * 判断是否是超级管理员
     * @return bool
     */
    public function isSuperAdministrator()
    {
        $result = false;
        if($this->getRoleAlias() == 'superAdministrator'){
            $result = true;
        }
        return $result;
    }

    /**
     * 判断是否是设计师
     * @return bool
     */
    public function isDesigner()
    {
        $result = false;
        if($this->getRoleAlias() == 'designer' || $this->getRoleAlias() =='designDirector'){
            $result = true;
        }
        return $result;
    }

    /**
     * 设计总监
     *
     */
    public function isDesignDirector(){
        $result = false;
        if($this->getRoleAlias() =='designDirector'){
            $result = true;
        }
        return $result;
    }
    /**
     * 判断是否是工地监理
     * @return bool
     */
    public function isEngineeringDepartmentSupervision()
    {
        $result = false;
        if($this->getRoleAlias() == 'engineeringDepartmentSupervision'){
            $result = true;
        }
        return $result;
    }
    /**
     * 判断后台是否可以登录
     * @return bool
     */
    public function isAdminLogin(){
        $result = $this->getIsAdminLogin();
        return  $result == 1?true:false;
    }
}